using System.Collections.Generic;
using System.Threading.Tasks;
using WPFServices.Models;
using WPFServices.Models.Domain;
using WPFServices.Services;

namespace WPFServices.Services.DomainService.WorkOrder
{
    public class WorkOrderService
    {
        private readonly ApiClient _apiClient;

        public WorkOrderService()
        {
            _apiClient = new ApiClient();
        }

        public async Task<ApiPaging<List<WorkOrderDto>>> GetWorkOrderListAsync(SearchWorkOrderDto searchDto)
        {
            try
            {
                var queryParams = new List<string>();
                
                if (!string.IsNullOrWhiteSpace(searchDto.OrderNumber))
                    queryParams.Add($"OrderNumber={searchDto.OrderNumber}");
                
                if (!string.IsNullOrWhiteSpace(searchDto.OrderName))
                    queryParams.Add($"OrderName={searchDto.OrderName}");
                
                if (!string.IsNullOrWhiteSpace(searchDto.PlanName))
                    queryParams.Add($"PlanName={searchDto.PlanName}");
                
                if (!string.IsNullOrWhiteSpace(searchDto.ProductName))
                    queryParams.Add($"ProductName={searchDto.ProductName}");
                
                if (searchDto.Status.HasValue)
                    queryParams.Add($"Status={searchDto.Status.Value}");
                
                queryParams.Add($"PageIndex={searchDto.PageIndex}");
                queryParams.Add($"PageSize={searchDto.PageSize}");

                var queryString = string.Join("&", queryParams);
                var url = $"Plan/GetWorkOrderList?{queryString}";

                var result = await _apiClient.GetAsync<ApiPaging<List<WorkOrderDto>>>(url);
                if (result.IsSuc && result.code == ResultCode.Ok)
                {
                    return result.data;
                }
                                 else
                 {
                     throw new Exception($"获取工单列表失败: {result.msg}");
                 }
            }
            catch (System.Exception ex)
            {
                throw new System.Exception($"获取工单列表失败: {ex.Message}");
            }
        }
    }
} 